package com.aaa.interceptor;

import com.aaa.unilt.JwtUntil;
import com.aaa.vo.Result;
import com.alibaba.fastjson.JSON;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author C-Q
 * @version 1.1.0
 * @Description: TODO
 * @date 2022/11/23
 */
public class MyInterceptor implements HandlerInterceptor {
    @Override
     public  boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setContentType("application/json;charset=utf-8");
        //1获取请求头的token
        String token=request.getHeader("token");
        //2判断token是否为null(内容)
        if(StringUtils.hasText(token)){
            //校验token是否合法
            if(JwtUntil.verifyToken(token)){
                return true;
            }
        }
        PrintWriter writer = response.getWriter();
        Result result = new Result(4001, "未登录");
        String s = JSON.toJSONString(result);
        writer.println(s);
        writer.flush();
        writer.close();
        return false;
    }


}
